package com.sky.controller.admin;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Api(tags = "分类相关接口")
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    @ApiOperation("分类修改")
    @PutMapping
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO){
        return categoryService.updateCategory(categoryDTO);
    }


    @ApiOperation("分类分页查询")
    @GetMapping("/page")
    public Result getCategoryPage(CategoryPageQueryDTO categoryPageQueryDTO){
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        Page<Category> page = categoryService.getCategoryPage(categoryPageQueryDTO);
        List<Category> result = page.getResult();
        PageResult pageResult = new PageResult(page.getTotal(), result);
        return Result.success(pageResult);
    }


    @ApiOperation("分类状态启用禁用")
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable("status") Integer status, Long id){
        return categoryService.updateStatus(status, id);
    }


    @ApiOperation("新增分类")
    @PostMapping
    public Result addCategory(@RequestBody CategoryDTO categoryDTO){
        return categoryService.addCategory(categoryDTO);
    }



    @ApiOperation("删除分类")
    @DeleteMapping
    public Result addCategory(@RequestParam("id") ArrayList<Long> ids){
        return categoryService.delCategoryBy(ids);
    }


    @ApiOperation("根据类型查询分类")
    @GetMapping("/list")
    public Result queryByType(@RequestParam("type") Integer type){
        return categoryService.queryByType(type);
    }
}
